package win.smartown.app.gank.type.data;

import java.util.ArrayList;
import java.util.List;

import win.smartown.app.base.mvp.BasePresenter;
import win.smartown.app.gank.entity.Data;
import win.smartown.app.gank.entity.GankResponse;
import win.smartown.app.request.RequestListener;
import win.smartown.app.request.RequestManager;

/**
 * Created by smartown on 2017/5/5 10:27.
 * Desc:
 */
public class TypeDataPresenter extends BasePresenter<Contract.View> implements Contract.Presenter {

    private String type;
    private int size;
    private int page;
    private List<Data> dataList;

    public TypeDataPresenter(Contract.View view, String type) {
        super(view);
        this.type = type;
        this.size = 10;
        this.dataList = new ArrayList<>();
    }

    public void refresh() {
        page = 0;
        dataList.clear();
        getView().onDataChanged();
        getNextPage();
    }

    @Override
    public void getNextPage() {
        page++;
        getView().getSuperActivity().request(RequestManager.GankManager.getDataByType(type, size, page), new RequestListener<GankResponse<List<Data>>>() {
            @Override
            public void onStart() {
                getView().startLoading();
            }

            @Override
            public void onFail(Throwable e) {
                getView().finishLoading();
            }

            @Override
            public void onSuccess(GankResponse<List<Data>> result) {
                getView().finishLoading();
                if (!result.isError()) {
                    dataList.addAll(result.getResults());
                    getView().onDataChanged();
                }
            }
        });
    }

    public List<Data> getDataList() {
        return dataList;
    }
}
